package com.scm.controller;

/**
 * @author CHL
 * @ClassName: StockController
 * @Description: TODO
 * @date 2022/3/8 14:34
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.ProductMapper;
import com.scm.dao.StockRecordMapper;
import com.scm.entity.CheckStock;
import com.scm.entity.Pomain;
import com.scm.entity.Product;
import com.scm.entity.Somain;
import com.scm.entity.StockRecord;
import com.scm.entity.response.ResponseMessage;
import com.scm.service.IPomainService;
import com.scm.service.ISomainService;
import com.scm.service.IStockService;
import com.scm.util.DateUtil;
import com.scm.util.StringUtil;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/stock"})
public class StockController
{
    private ResponseMessage rm = new ResponseMessage();
    @Autowired
    private IStockService stockService;
    @Autowired
    private IPomainService pomainService;
    @Autowired
    private ISomainService somainService;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StockRecordMapper stockRecordMapper;

    @RequestMapping({"/instock"})
    public ResponseMessage instock(Long poId, Integer page, Integer payType, HttpServletRequest req)
    {
        String account = (String)req.getAttribute("login");
        if (StringUtil.isEmpty(poId))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("入库采购单编号不能为空");
            return this.rm;
        }
        if (StringUtil.isEmpty(payType))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("入库采购单付款方式不能为空");
            return this.rm;
        }
        this.stockService.instock(poId.longValue(), account);
        PageInfo<Pomain> info = this.pomainService.selectByStatus(page, Integer.valueOf(2), payType);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("入库成功");
        this.rm.setData(info);
        return this.rm;
    }

    @RequestMapping({"/outstock"})
    public ResponseMessage outstock(Long soId, Integer page, Integer payType, HttpServletRequest req)
    {
        if (StringUtil.isEmpty(soId))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("出库编号不能为空");
            return this.rm;
        }
        if (StringUtil.isEmpty(payType))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("出库付款方式不能为空");
            return this.rm;
        }
        String user = (String)req.getAttribute("login");
        this.stockService.outstock(soId.longValue(), user);
        PageInfo<Somain> info = this.somainService.selectByStatus(page, 2, payType);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("出库成功");
        this.rm.setData(info);
        return this.rm;
    }

    @RequestMapping({"/checkstock"})
    public ResponseMessage checkStock(CheckStock cs, Integer num, HttpServletRequest req)
    {
        if (StringUtil.isEmpty(cs.getProductCode()))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品编号不能为空");
            return this.rm;
        }
        Integer originNum = cs.getOriginNum();
        if (StringUtil.isEmpty(originNum))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品原始数量不能为空");
            return this.rm;
        }
        String type = cs.getType();
        if (StringUtil.isEmpty(type))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品损益类型不能为空");
            return this.rm;
        }
        if (StringUtil.isEmpty(cs.getDescription()))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品损益原因不能为空");
            return this.rm;
        }
        if (StringUtil.isEmpty(num))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品变化数量不能为空或0");
            return this.rm;
        }
        String user = (String)req.getAttribute("login");
        num = Integer.valueOf(cs.getType().equals("损耗") ? -num.intValue() : num.intValue());
        cs.setRealNum(Integer.valueOf(originNum.intValue() + num.intValue()));
        cs.setCreateUser(user);
        cs.setStockTime(DateUtil.currentTime());
        this.stockService.checkStock(cs, num.intValue());
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("成功");
        return this.rm;
    }

    @RequestMapping({"/query"})
    public PageInfo<Product> queryStock(String productCode, String name, Integer min, Integer max, Integer page)
    {
        HashMap<String, Object> m = new HashMap();
        m.put("productCode", productCode);
        m.put("name", name);
        m.put("min", min);
        m.put("max", max);
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        PageInfo<Product> info = new PageInfo(this.productMapper.selectStock(m));

        return info;
    }

    @RequestMapping({"/alterRecord"})
    public ResponseMessage queryAlterRecord(String productCode, Integer stockType, Integer page)
    {
        if (StringUtil.isEmpty(productCode))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品编号不能为空");
            return this.rm;
        }
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        m.put("stockType", stockType);
        m.put("productCode", productCode);
        PageInfo<StockRecord> info = new PageInfo(this.stockRecordMapper.selectStockRecordByProductCode(m));
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("修改成功");

        this.rm.setData(info);
        return this.rm;
    }
}

